<template>
	<view class="address">
		<common-list :isDataEmpty="tableData.length === 0" :isLoad="false" :status="status">
			<template #list>
				<view>
					<view class="list">
						<view class="card" v-for="(item, index) in tableData" :key="item.id" @click="handChoose(item)">
							<view class="userinfo">
								<view class="name">{{ item.name }}</view>
								<view class="phone">{{ item.cellphone }}</view>
								<view class="tag" v-if="item.isDefault">默认</view>
							</view>
							<view class="descs">
								<text>{{ item.provinceName }}</text>
								<text>{{ item.cityName }}</text>
								<text>{{ item.countyName }}</text>
								<text>{{ item.address }}</text>
							</view>
							<view class="card-bottom">
								<uv-checkbox-group v-model="checkboxValue" shape="circle"
									@change="defaultChange($event, item)">
									<uv-checkbox activeColor="#FFC434" label="默认地址" :name="item.id"></uv-checkbox>
								</uv-checkbox-group>
								<view class="tool">
									<view class="btn" @click.stop="handAdd(item, index)">
										<image src="../../static/address/edit.png" mode=""></image>
										<text>编辑</text>
									</view>
									<view class="btn" @click.stop="handDel(index)">
										<image src="../../static/address/del.png" mode=""></image>
										<text>删除</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</template>
			<template #empty>
				<view class="">
					<view class="empty">
						<image src="/static/addressEmpty.png" mode="widthFix"></image>
						<text>您暂时还没有添加地址，快去添加吧~</text>
					</view>
				</view>
			</template>
		</common-list>


		<view class="add-btn">
			<button @click="handAdd()">
				<u-icon name="plus"></u-icon>
				<text style="margin-left: 10rpx;">添加收货地址</text>
			</button>
		</view>

		<uv-modal ref="modal" show-cancel-button confirm-color="#FF9113" @confirm="handConfirm" width="550rpx">
			<view style="line-height: 100rpx;">确认要删除吗?</view>
		</uv-modal>
	</view>
</template>

<script setup>
	import {
		ref,
		getCurrentInstance,
		inject,
		toRaw,
		nextTick
	} from 'vue'
	import commonList from "@/components/commonList/list.vue"
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		doGetList,
		doDel,
		doEdit
	} from '@/apis/address.js'
	const {
		proxy
	} = getCurrentInstance();
	const $back = inject('$back')


	const modal = ref()
	let oldIndex = 0
	const handDel = (index) => {
		oldIndex = index
		modal.value.open()
	}
	const handConfirm = async () => {
		await doDel(tableData.value[oldIndex].id)
		tableData.value.splice(oldIndex, 1)
	}
	let type = ''
	const handChoose = (e) => {
		if (type === 'choose') {
			const eventChannel = proxy.getOpenerEventChannel()
			eventChannel.emit('someEvent', toRaw(e));
			$back()
		}
	}
	const handAdd = (e, index) => {
		uni.navigateTo({
			url: '/pagesA/address/add',
			events: {
				successEvent: (info) => {
					if (e) {
						tableData.value[index] = info
					} else {
						fenctData()
					}
				}
			},
			success: (res) => {
				if (e) {
					res.eventChannel.emit('editData', uni.$uv.deepClone(e))
				}
			}
		})
	}

	const checkboxValue = ref([])
	const tableData = ref([])
	const status = ref('loading')
	const fenctData = async () => {
		status.value = 'loading'
		const data = await doGetList()
		tableData.value = data
		status.value = 'nomore'
		const defaultInfo = data.find(el => el.isDefault)
		if (defaultInfo) {
			checkboxValue.value = [defaultInfo.id]
		} else {
			checkboxValue.value = []
		}
	}

	const defaultChange = async (e, row) => {
		nextTick(() => {
			if (e.length === 0) {
				const defaultInfo = tableData.value.find(el => el.isDefault)
				if (defaultInfo) {
					checkboxValue.value = [defaultInfo.id]
				} else {
					checkboxValue.value = []
				}
			} else {
				row.isDefault = true
				tableData.value.map(el => {
					el.isDefault = el.id === e[0]
					return el
				})
				doEdit(row)
			}
		})
	}

	onLoad((e) => {
		if (e?.type) {
			type = e?.type
		}
		fenctData()
	})
</script>

<style lang="scss" scoped>
	.address {
		min-height: 100vh;
		background: #F5F5F5;

		.header {
			display: flex;
			flex-direction: column;
			position: fixed;
			top: 0;
			width: 750rpx;
			z-index: 5;
			padding-bottom: 30rpx;

			.bg {
				position: absolute;
				top: 0;
				left: 0;
				z-index: 1;
				width: 750rpx;
			}


			&-main {
				flex: 1;
				display: flex;
				align-items: center;
				padding-left: 20rpx;
				position: relative;
				z-index: 2;

				.back {
					image {
						width: 18rpx;
						height: 33rpx;
					}
				}

				.title {
					flex: 1;
					text-align: center;
					font-size: 36rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
				}
			}
		}

		.empty {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			padding-top: 20rpx;

			image {
				width: 583.3rpxrpx;
				height: auto;
			}

			text {
				margin-top: 70rpx;
				font-size: 26rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
			}
		}

		.list {
			padding: 32rpx;

			.card {
				width: 686rpx;
				margin: 0 auto;
				background-color: #fff;
				border-radius: 16rpx;
				box-sizing: border-box;
				padding: 24rpx 30rpx;

				&:not(:first-child) {
					margin-top: 32rpx;
				}

				.userinfo {
					display: flex;
					align-items: center;

					.name {
						font-size: 30rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #06121E;
					}

					.phone {
						margin-left: 54rpx;
						font-size: 30rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #06121E;
					}

					.tag {
						font-size: 24rpx;
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #FF930D;
						padding: 2rpx 10rpx;
						background: #FFF7E4;
						border-radius: 6rpx;
					}
				}

				.descs {
					margin-top: 22rpx;
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
				}

				&-bottom {
					margin-top: 30rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding-top: 26rpx;
					border-top: 2rpx solid #DDDDDD;

					.tool {
						display: flex;
						align-items: center;

						.btn {
							display: flex;
							align-items: center;

							&:last-child {
								margin-left: 60rpx;
							}

							image {
								width: 26rpx;
								height: 26rpx;
							}

							text {
								margin-left: 8rpx;
								font-size: 24rpx;
								font-family: PingFang SC-Regular, PingFang SC;
								font-weight: 400;
								color: #999999;
							}
						}
					}
				}
			}
		}

		.add-btn {
			position: fixed;
			bottom: 80rpx;
			width: 750rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			button {
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #333333;
				width: 694rpx;
				height: 104rpx;
				background-color: #FFC434;
				border-radius: 24rpx;

				&::after {
					border: none;
				}
			}
		}
	}
</style>